package com.lgt.fishmall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.lgt.fishmall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000119649031";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCPmN5qVP3P9om/g9R+lZZd2x9r11fbSJAI6CTDFSU49OMXhlN3tIG6WSjRB9lyjTQUW/1y8b7B9ue7BVgtrfhpWTtchf/ouRb50/eV0NiP3J3cy3jXTfmPEpvNrV3VFs9K41HLy71237peOcdsk5oQ06wbDclhTz4CteEl9poYDhOuc2eorOpZSlxMYc0WoqPG/3aJ3yEpUeLHsq1NBV/PF4JW4LsJGSVt4262/etEt2G/g8gDH4t2sczzIOgqnxEvdPAjvvHHV6Z1j8UccprKT3oGhMpNZXpdCXV9Xba7lEhu3c7FHFXY1Ameq0TUacufM7mYQG6YZQbNXYB/GeIVAgMBAAECggEAR0fHAFl4NIdZEWxLSxrOLYDcNBgNUJjQhttVhFE2zPMi10aioEVGME6Z6ElRM3/MXl7bjPdbjcUWesvIXcSCDylwQhlyUv+t3A3QVNn65k7JPhTyw029l7vRA2QPs/tCoxX8QIG+Zwff524fBFKwo7GvOS66zEcdrRLD1Kh5DX8PJpJ3fY3O30oeo9pIzyKZrOPvlb69ox7Z1mFCzJ+0fEl837sCsyABKcBK3Jvh758vsjWXjPj39m2T+Fj4wBn3Ye3uVwG5F+45K/V/+vTsB2pXo8K6wtSIoBexKbb1V8Bs8AR1NgVgF4QuiR+Q3QbKWCwcU5IjP7fa1OCQWVCicQKBgQDchmBHhkHnxh3//lgTBld23pl1rU5xMOignDDxBEc8azP6DQxnPRvilVNj6RlZQ+SjwwnSRAX54Bu+OkGqzVTweXUzftWBsdhkBCXfqGtGbl38qULGXEiTf9sua9tvXQ781pVMsYkStVq7Shag+w05sFhrHzQOdZri9dwomKlHOwKBgQCmsncMPbK166V/y4dALLjFWn0ij1WkVz8iltFky6qE/98t8NBvw0fLsVzbNFPyQ0sNK65q8g57RqeaRlInCfsYBjD23mNIiUYW1KN/x6e2F3Z8d2V4RHf/6h19mz//C8EoltsV3x1dil54wC7UKUCPjIkd8DMh9G51tPrlj98G7wKBgQCYQgs9tvvQDCDwe+rRj+JZx02LS1JKoqsrIUxFTF/m3lmvvsyehzm2ksVVyT5gPJkjXGlujNrXcJ2XFN/8DvMjaai7GkpN1r4CKD8CEmWm5lLsPO1GybauaGurvHpwhH7xoTLKFhX/Hcn8YCYJlezGtPQHWi72AMslAv2z5duSuwKBgQCICX6Du9obAlrGZudSZOzJCeazDHwcqKK6bJAnjNn0vgd5K1w+yQPPZEfeD98uBuK5tdsaJb3RtsBSc27Zsxh565uL68LkUDqlqRRg1CO2w4ELZDinpGC1tW4pskfQsNwKbsoOGE2Tp7cfHd9ZfuzT+SFxEDP6UsrOPkq+iuG8ewKBgQCdZ3eh0vZv1eab01Wp59qYnO9UU98Cy/1aLCqR/8KDBX4PDvTfmB8WSXfMcQGxczB6kxZpo0AATQNf09PaTaCynbt2oAmnJDqFCy7rb0Sm7L8hjE/AGs9zDrSmpPoBvoIMsgRb/4YZupXAoIw/I4uzoUEK5D7nugMeEOOJAyHXzA==";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjSuMGbKU/ICvomD3yKmD+WXsCFkdTdLvGlIE41bkyp9w1ezLp5nY8U8mxrZCTM+avIFVS62zuYzdA44fiUNkh5lk+m5GwRHVTbUwvdYXyIE2VmYYMAp91dEC3lhkxexvtt0ymPrWJoXmir3PVZ62EFpwW+5+X78MLSSnw7ZegDPeIaEovzRhpZXPB1Y8RnFXPDyYzKkvGY5gFYslBx0lwOzNNxpPF1/SmAXjJJuAO+KI4d4BE9tLvjRw+UOurYdMrNNIwVEV7bzO8eGkSY7NXdfcfBmMS3i/Pf2r7CM67zS8FidquDhMlx6Oq3zCPcjp5ivdH1gQOcikCVWrOA/W0QIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://507sz73569.wicp.vip/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url ="http://member.fishmall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"1m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
